package cn.zhp.service.impl;

import cn.zhp.entity.SysRole;
import cn.zhp.mapper.SysRoleMapper;
import cn.zhp.service.SysRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole> implements SysRoleService {


    @Autowired
    SysRoleMapper roleMapper;


    @Override
    public PageInfo<SysRole> selectPage(int pageNum,int pageSize,Map<String, Object> params) {

        PageHelper.startPage(pageNum,pageSize);
        List<SysRole> sysRoles = roleMapper.selectPage(params);
        PageInfo<SysRole> pageInfo = new PageInfo<>(sysRoles);//生成分页对象

        return pageInfo;
    }


    @Override
    public int deleteBatch(long rid, long[] ids){
        return roleMapper.deleteBatch(rid,ids);
    }

    @Override
    public int insertBatch( long rid,List<Long> cids){
        return roleMapper.insertBatch(rid,cids);
    }

    /**
     * 更新role：
     * 1.更新role本表信息
     * 2.更新role对应的resources   如果为新旧一致则不更新
     * 3.更新role对应的offices       如果为新旧一致则不更新
     * @param sysRole
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(SysRole sysRole) {

        int result = 0;

        sysRole.setUpdateDate(new Date());
        roleMapper.updateByPrimaryKeySelective(sysRole);
        result++;
        Long id = sysRole.getId();


        if (sysRole.getResources()!=null&&sysRole.getOldResources()!=null
                &&sysRole.getResources().size()==sysRole.getOldResources().size()) {
            if(!CollectionUtils.containsAll(sysRole.getResources(),sysRole.getOldResources())){
                roleMapper.deleteByRid(id);
                roleMapper.insertRoleResources(id,sysRole.getResources());
            }
        }else {
            roleMapper.deleteByRid(id);
            if(sysRole.getResources()!=null) { //当为null则是清空角色授权
                roleMapper.insertRoleResources(id, sysRole.getResources());
            }
        }

        if (sysRole.getOffices()!=null&&sysRole.getOldOffices()!=null
                &&sysRole.getOffices().size()==sysRole.getOldOffices().size()) {
            if(!CollectionUtils.containsAll(sysRole.getOffices(),sysRole.getOldOffices())){
                roleMapper.deleteOfficeByRid(id);
                roleMapper.insertRoleOffices(id,sysRole.getOffices());
            }
        }else {
            roleMapper.deleteOfficeByRid(id);
            if(sysRole.getOldOffices()!=null){//当为null则是清空角色跨机构授权
                roleMapper.insertRoleOffices(id,sysRole.getOffices());
            }

        }
       /* if(sysRole.getOffices()!=null&&sysRole.getOffices().size()>0){
            roleMapper.deleteOfficeByRid(id);
            roleMapper.insertRoleOffices(id,sysRole.getOffices());
            result++;
        }

        if(sysRole.getResources()!=null&&sysRole.getResources().size()>0){
            roleMapper.deleteByRid(id);
            roleMapper.insertRoleResources(id,sysRole.getResources());
            result++;
        }*/

        return result;
    }
}
